package com.janyee.powerdroid.http.local

import com.alibaba.fastjson.annotation.JSONField

import java.io.IOException

import okhttp3.Headers
import okhttp3.Interceptor
import okhttp3.MediaType
import okhttp3.Protocol
import okhttp3.Response
import okhttp3.ResponseBody

/**
 * Created by kmlixh on 2017/2/23.
 * useage:
 * PowerDroid
 */

class LocalResponse {
    var code: Int = 0
    var message: String?=null
    var protocol: String?=null
    var mediaType: String?=null
    var body: String?=null
    var headers: Map<String, String>?=null
    fun toResponse(chain: Interceptor.Chain): Response {
        return Response.Builder()
                .code(code)
                .message(message)
                .request(chain.request())
                .protocol(getProtocal(protocol!!))
                .body(ResponseBody.create(MediaType.parse(mediaType), body))
                .headers(Headers.of(headers))
                .sentRequestAtMillis(System.currentTimeMillis())
                .receivedResponseAtMillis(System.currentTimeMillis())
                .build()
    }

    companion object {
        fun getProtocal(name: String): Protocol {
            var protocol: Protocol
            try {
                protocol = Protocol.get(name)
            } catch (e: IOException) {
                protocol = Protocol.HTTP_1_0
            }

            return protocol
        }
    }
}
